package com.zh.api.controller.supervise;


import com.alibaba.fastjson.JSONObject;
import com.zh.common.base.BaseController;
import com.zh.common.utils.*;
import com.zh.supervise.domain.VisitOldDO;
import com.zh.supervise.service.VisitOldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author lds
 * @date 2019-05-15 16:38:30
 */
 
@Controller
@RequestMapping("/supervise/visit")
@Api("走访管理")
public class VisitOldController extends BaseController{

	@Autowired
	private VisitOldService visitOldService;


	
	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("获取走访列表")
	public ResultVO list(@RequestBody Map<String, Object> params){

		if (CheckEmptyUtil.isEmpty(params)) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		//查询列表数据
        Query query = new Query(params);
		List<VisitOldDO> visitOldList = visitOldService.list(query);
		int total = visitOldService.count(query);
		PageUtils pageUtils = new PageUtils(visitOldList, total);
		return ResultVO.success(pageUtils);
	}


	@ResponseBody
	@RequestMapping(value = "getVisitById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("获取走访详情")
	ResultVO getVisitById(@RequestBody VisitOldDO visitOldDO){
		if(visitOldDO==null||CheckEmptyUtil.isEmpty(visitOldDO.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		VisitOldDO visitOld = visitOldService.get(visitOldDO.getId());
		return ResultVO.success(visitOld);
	}
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping(value = "save", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("保存走访信息")
	public ResultVO save(@RequestBody VisitOldDO visitOld){

		if(visitOld==null ||CheckEmptyUtil.isEmptys(visitOld.getVisitUserName()
		,visitOld.getOldUserId(),visitOld.getVisitDeptId())){

			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		if(visitOldService.save(visitOld)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping(value = "update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("编辑走访信息")
	public ResultVO update(@RequestBody VisitOldDO visitOld){

		if(visitOld==null ||CheckEmptyUtil.isEmptys(visitOld.getId(),visitOld.getVisitUserName()
				,visitOld.getOldUserId(),visitOld.getVisitDeptId())){

			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		if(visitOldService.update(visitOld)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	/**
	 * 删除
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("删除走访信息")
	public ResultVO remove(@RequestBody VisitOldDO visitOld){
		if(visitOld==null||CheckEmptyUtil.isEmpty(visitOld.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}


		if(visitOldService.remove(visitOld.getId())>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	/**
	 * 删除
	 */
	@RequestMapping(value = "batchRemove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除走访信息")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		if(jsonParam==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}


		List<String> ids= (List<String>) jsonParam.get("ids");

		if(CheckEmptyUtil.isEmpty(ids)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		String[] strs1=ids.toArray(new String[ids.size()]);

		if(visitOldService.batchRemove(strs1)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	
}
